package com.robot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.robot.common.enums.IsDeleteEnum;
import com.robot.common.result.PageResult;
import com.robot.common.utils.MinioUtils;
import com.robot.dao.dto.KnowledgeDTO;
import com.robot.dao.dto.KnowledgePageQueryDTO;
import com.robot.dao.dto.KnowledgePageQueryFileDTO;
import com.robot.dao.entity.File;
import com.robot.dao.entity.Knowledge;
import com.robot.dao.entity.UserKnowledge;
import com.robot.dao.mapper.KnowledgeMapper;
import com.robot.dao.vo.FileVO;
import com.robot.dao.vo.KnowledgePageQueryVO;
import com.robot.service.KnowledgeService;
import com.robot.service.UserKnowledgeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class KnowledgeServiceImpl extends ServiceImpl<KnowledgeMapper, Knowledge> implements KnowledgeService {
    //文件上传缓冲bucket
    private static final String BUFFER_ID = "1821091408614404171";
    private static final String BUFFER_BUCKET_NAME = "1274682200419205120";

    @Autowired
    private UserKnowledgeService userKnowledgeService;
    @Autowired
    private KnowledgeMapper knowledgeMapper;
    @Autowired
    private MinioUtils minioUtils;

    @Transactional
    public Knowledge query(KnowledgeDTO knowledgeDTO) {
        Knowledge knowledge = new Knowledge();
        BeanUtils.copyProperties(knowledgeDTO, knowledge);
        LambdaQueryWrapper<Knowledge> knowledgeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        knowledgeLambdaQueryWrapper.eq(Knowledge::getId, knowledge.getId());
        return super.getOne(knowledgeLambdaQueryWrapper);
    }

    @Transactional
    public Knowledge createknowledge(KnowledgeDTO knowledgeDTO) {
        Knowledge knowledge = new Knowledge();
        BeanUtils.copyProperties(knowledgeDTO, knowledge);
        knowledge.setApplicationNumber(0);
        knowledge.setFileNumber(0);
        knowledge.setIsDelete(IsDeleteEnum.EXIST.getValue());
        String bucketName = minioUtils.existBucketPartial(String.valueOf(knowledge.getId()));
        knowledge.setMinioBucketName(bucketName);
        super.save(knowledge);
        Long knowledgeid = knowledge.getId();
        UserKnowledge userKnowledge = new UserKnowledge();
        userKnowledge.setUserId(new Long(knowledgeDTO.getUserId()));
        userKnowledge.setKnowledgeId(knowledgeid);
        userKnowledgeService.save(userKnowledge);
        return knowledge;
    }

    @Transactional
    public PageResult pageQuery(KnowledgePageQueryDTO knowledgePageQueryDTO) {
        int page = (knowledgePageQueryDTO.getPage() == 0) ? 1 : knowledgePageQueryDTO.getPage();
        int pageSize = (knowledgePageQueryDTO.getPageSize() <= 0) ? 10 : knowledgePageQueryDTO.getPageSize();

        PageHelper.startPage(page, pageSize);
        Page<Knowledge> pageResult = knowledgeMapper.pageQuery(Long.valueOf(knowledgePageQueryDTO.getUserId()), knowledgePageQueryDTO.getKnowledgeType(), knowledgePageQueryDTO.getKnowledgeName());

        List<Knowledge> robots = pageResult.getResult();
        List<KnowledgePageQueryVO> list = new ArrayList<>();
        for (Knowledge knowledge : robots) {
            KnowledgePageQueryVO knowledgePageQueryVO = new KnowledgePageQueryVO();
            BeanUtils.copyProperties(knowledge, knowledgePageQueryVO);
            knowledgePageQueryVO.setKnowledgeId(String.valueOf(knowledge.getId()));
            knowledgePageQueryVO.setRobotNumber(knowledge.getApplicationNumber());
            list.add(knowledgePageQueryVO);
        }

        boolean hasMore = page < pageResult.getPages();
        return new PageResult(pageResult.getTotal(), hasMore, list);


    }

    @Transactional
    public PageResult pageQueryfile(KnowledgePageQueryFileDTO knowledgePageQueryFileDTO) {
        PageHelper.startPage(knowledgePageQueryFileDTO.getPage(), knowledgePageQueryFileDTO.getPageSize());
        Page<File> page = knowledgeMapper.pageQueryFile(new Long(knowledgePageQueryFileDTO.getKnowledgeId()), knowledgePageQueryFileDTO.getFileName());

        boolean hasMore = false;
        long total = page.getTotal();
        Long totalPages = (long) Math.ceil(total / knowledgePageQueryFileDTO.getPageSize());
        if (total % knowledgePageQueryFileDTO.getPageSize() > 0) {
            totalPages = totalPages + 1;
        }
        if (totalPages != knowledgePageQueryFileDTO.getPage()) {
            hasMore = true;
        }

        List<FileVO> list = new ArrayList<>();
        for (File file : page) {
            FileVO fileVO = new FileVO();
            BeanUtils.copyProperties(file, fileVO);
            fileVO.setFileId(String.valueOf(file.getId()));
            list.add(fileVO);
        }


        return new PageResult(page.getTotal(), hasMore, list);
    }


}
